package com.javaee.ch11.service;


import com.javaee.ch11.common.exceptions.ServiceException;
import com.javaee.ch11.entity.Book;
import com.javaee.ch11.mapper.BookMapper;
import com.javaee.ch11.web.dto.BookQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public List<Book> listAll() {
        return bookMapper.selectAll();
    }

    public Book getById(Long id) {
        return bookMapper.selectById(id);
    }

    public void add(Book book) {
        if (bookMapper.insert(book) != 1) {
            throw new ServiceException("图书添加失败");
        }

    }

    public void updateById(Book book) {
        if (bookMapper.updateById(book) != 1) {
            throw new ServiceException("图书更新失败");
        }
    }

    public void deleteById(Long id) {
        if (bookMapper.deleteById(id) != 1) {
            throw new ServiceException("图书删除失败");
        }
    }

    public List<Book> queryBooks(BookQueryDto queryDto) {
        return bookMapper.queryBooks(queryDto);
    }
}